package view;

import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;

import model.Card;
import model.Colors;

public class CardView {

	private View view;
	private Card card;
	private boolean visible;
	private boolean frontView;
	private boolean onTheTable;
	private int numberOfPlayers;
	
	public CardView(Card card, View view,int numberOfPlayers) {
		this.card = card;
		this.view = view;
		this.numberOfPlayers = numberOfPlayers;
	}
	
	private void checkCard() {
		if (card.getPlayer() == -2)
			visible = false;
		else if (card.getPlayer() != -3)
			visible = true;
		if ((card.getPlayer() == 0) || (card.getPlayer() == -1))
			frontView = true;
		else
			frontView = false;
		if (card.getPlayer() == -1)
			onTheTable = true;
		else
			onTheTable = false;
	}
	
	public Card getCard() {
		return card;
	}

	public void paint(Graphics g, int k) {
		checkCard();
		
		if (visible) {
			if (frontView) {
				int posX, posY;
				
				if (onTheTable) {
					posX = (16-numberOfPlayers)*32+k*70;
					posY = 230;
				}
				else {
					PlayerView player = view.getPlayerView(card.getPlayer());
					posX = player.getCardPositionX(card);
					posY = player.getPositionY()-110;
				}
				
				if (card.isSelected() && card.getPlayer() == 0) {
					posX += 5;
					posY -= 20;
				}
			
				if (card.getColor() == Colors.Club) {
					Image image = new ImageIcon("Cards/Clubs/" + card.getValue() + ".png").getImage();
					g.drawImage(image, posX, posY, null);
				}
				else if (card.getColor() == Colors.Spade) {
					Image image = new ImageIcon("Cards/Spades/" + card.getValue() + ".png").getImage();
					g.drawImage(image, posX, posY, null);
				}
				else if (card.getColor() == Colors.Diamond) {
					Image image = new ImageIcon("Cards/Diamonds/" + card.getValue() + ".png").getImage();
					g.drawImage(image, posX, posY, null);
				}
				else {
					Image image = new ImageIcon("Cards/Hearts/" + card.getValue() + ".png").getImage();
					g.drawImage(image, posX, posY, null);
				}
			}
			else {
				PlayerView player = view.getPlayerView(card.getPlayer());
				Image image = new ImageIcon("Cards/black_back.png").getImage();
				if (player != null)
					g.drawImage(image, player.getPositionX()-20, player.getPositionY()+20, null);
			}
		}
	}

}